package com.cqgcxy.shopping.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqgcxy.shopping.common.CommException;
import com.cqgcxy.shopping.domain.Getaddress;
import com.cqgcxy.shopping.service.AddressService;


@WebServlet(urlPatterns="/user/getaddress.do")
public class AddressServlet extends HttpServlet {

	private static final long serialVersionUID = 4435873782480039747L;
	private Logger  logger = LoggerFactory.getLogger(getClass());
	private AddressService addressService = new AddressService();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		logger.info("1111");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		logger.info("login.dopost");
		try {
			// 1.抽取参数，封装为实体对象
			String area=req.getParameter("area");
			String address=req.getParameter("address");
			String consigneename=req.getParameter("consigneename");
			String consigneephonenum=req.getParameter("consigneephonenum");
			String postcode=req.getParameter("postcode");
			String usercode=req.getParameter("usercode");
			logger.debug("获取参数成功：{},{},{},{},{},{}",area,address,consigneename,consigneephonenum,postcode,usercode);
			// 2.类型转换
			logger.debug("类型转换成功");
			// 3.数据校验
			//地区不能为空
			if(area==null||area.trim().length()<=0) {
				throw new CommException("地区不能为空");
			}
			
			//地址不能为空
			if(address==null||address.trim().length()<=0) {
				throw new CommException("地址不能为空");
			}
			//收货人不能为空
			if(consigneename==null||consigneename.trim().length()<=0) {
				throw new CommException("收货人不能为空");
			}

			//收货人电话号码不能为空
			if(consigneephonenum==null||consigneephonenum.trim().length()<=0) {
				throw new CommException("收货人电话号码不能为空");
			}

			//邮编不能为空
			if(postcode==null||postcode.trim().length()<=0) {
				throw new CommException("邮编不能为空");
			}

			logger.debug("校验成功");
			//4.参数封装为对象
			Getaddress getaddress=new Getaddress();
			getaddress.setArea(area);
			getaddress.setAddress(address);
			getaddress.setConsigneename(consigneename);
			getaddress.setConsigneephonenum(consigneephonenum);
			getaddress.setPostcode(postcode);
			getaddress.setUsercode(usercode);
			HttpSession session=req.getSession();
			session.setAttribute("getaddress", getaddress);
			logger.debug("业务处理成功");
			addressService.add(getaddress);
			// 6.转发或者重定向
			resp.sendRedirect(req.getContextPath()+"/member/personal.jsp");//重定向
			logger.debug("添加成功");
	}catch (Exception e) {
		req.setAttribute("error", e.getMessage());
		req.getRequestDispatcher("/member/address.jsp").forward(req, resp);
		logger.info("添加失败:{}",e.getMessage());
	}
	}

}
